package edu.guigu.appquickstart.datastore.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import edu.guigu.appquickstart.datastore.dao.DBHelper;
import edu.guigu.appquickstart.datastore.dao.IUserDao;

/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/13 11:08
 */
public class UserBeanProvider extends ContentProvider {

    private DBHelper dbHelper = null;
    // 定义URI
    private static UriMatcher uriMatcher;
    private static final String AUTHORITY = "edu.guigu.appquickstart.datastore.user";

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        // content://edu.guigu.appquickstart.datastore.user/user
        // content://edu.guigu.appquickstart.datastore.user/user/2
        uriMatcher.addURI(AUTHORITY, "user", 119);
    }


    @Override
    public boolean onCreate() {
        dbHelper = DBHelper.getInstance(getContext());
        // 是否成功
        return dbHelper == null ? false : true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int code = uriMatcher.match(uri);
        Cursor cursor = null;
        // content://edu.guigu.appquickstart.datastore.user/user
        if (code == 119) {
//            cursor = dbHelper.queryAll(IUserDao.TABLE_USERS);
            cursor = dbHelper.query(IUserDao.TABLE_USERS,projection,selection,selectionArgs,null,null,sortOrder);
        }
        return cursor;
    }


    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return "";
    }
}
